package leetCode.offer57;

import java.util.Arrays;

public class Solution2 implements Solution{


    @Override
    public int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        for (int i=0;i<nums.length;i++ ) {
            int num = nums[i];
            if (binarySearch(nums, i+1,nums.length,target - num) == -2) {
                result[0] = num;
                result[1] = target - num;
                break;
            }
        }
        return result;
    }

    public static int binarySearch(int[] nums,int start ,int end, int target) {
        while (end>start) {
            int cur = (end+start)/2;
            if(target==nums[cur]){
                return -2;
            }
            if(target<nums[cur]){
                end = cur;
            }
            if(target>nums[cur]){
                start = cur+1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] parms = {16,16,18,24,30,32};
        System.out.println(Arrays.toString(new Solution2().twoSum(parms, 48)));
        System.out.println(binarySearch(parms,0,parms.length,19));
    }



}
